package hotgammon.variants.movestrategy;

import hotgammon.common.Color;
import hotgammon.common.Game;
import hotgammon.common.Location;
import hotgammon.common.MoveStrategy;

/**
 * Represents the AlphaMon moving rule
 * @author khanh.nhan
 *
 */
public class AlphaMoveStrategy implements MoveStrategy {
	@Override
	public boolean isMoveValid(Location from, Location to, Game game) {
		Color turn = game.getPlayerInTurn();
		Color fromColor = game.getColor(from);
		Color toColor = game.getColor(to);
		
		//if the game doesnt start, nobody can move
		if (turn == Color.NONE) {
			return false;
		}
		//check if color of moving checker matches with the checker's color in turn
		if (fromColor != turn){
			return false;
		}
		//check if they're moving a red to a black position or vice versa
		if((toColor != Color.NONE) && (toColor != turn)){
			return false;
		}
		//check if the direction is correct
		if (Location.distance(from, to) * turn.getSign() <= 0)
			return false;
		
		int absoluteDist = Math.abs(Location.distance(from, to));
		int[] unusedValues = game.diceValuesLeft();
		
		//TODO TODO TODO
		//check distance matches from diceValuesLeft with moving distance
		if (!contains(unusedValues, absoluteDist)) {
			return false;
		}
		
		return true;
	}

	/**
	 * Check if an int array contains a value or not
	 * @param array the array to check
	 * @param value the value
	 * @return true if the array contains the value
	 */
	private boolean contains(int[] array, int value) {
		for(int i : array) {
			if (value == i)
				return true;
		}
		return false;
	}

}
